package org.springframework.core.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public abstract class AbstractResource implements Resource {
    public boolean exists() {
        try {
            //先获取文件 如果存在返回true
            return getFile().exists();
        } catch (IOException e) {
            //文件不存在 尝试获取输入流
            try {
                //输入流获取没有异常则存在 返回true 否则返回false
                InputStream is = getInputStream();
                is.close();
                return true;
            } catch (Throwable isEx) {
                return false;
            }
        }

    }

    public File getFile() throws IOException {
        throw new FileNotFoundException("resource cannot be resolved to absolute file path");
    }

    public URL getURL() throws IOException {
        throw new FileNotFoundException("resource cannot be resolved to URL");
    }
}
